package io.renren.modules.xiaoqu.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.renren.modules.xiaoqu.entity.CostEntity;
import io.swagger.annotations.Api;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.xiaoqu.entity.ComplainEntity;
import io.renren.modules.xiaoqu.service.ComplainService;
import io.renren.common.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 反馈信息表
 *
 * @author yan
 * @email 2425784791@qq.com
 * @date 2022-05-19 21:56:53
 */
@Api
@RestController
@RequestMapping("xiaoqu/complain")
public class ComplainController {
    @Autowired
    private ComplainService complainService;

    @Autowired
    private UserService userService;

    /**
     * 列表
     */

    @PostMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.replace("page", params.get("page").toString());
        params.replace("limit", params.get("limit").toString());
        PageUtils page = complainService.queryPage(params);
        return R.ok().put("page", page);
    }

//    @GetMapping("/getWarrantyList")
//    public R getWarrantyList(String name, String master_name, Integer status,int page,int pageSize){
//
//        LambdaQueryWrapper<UserEntity> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(name),UserEntity::getUsername,name);
//
//        List<UserEntity> userEntityList = userService.list(queryWrapper);
//
//        List<Long> ids=new ArrayList<>();
//
//        for (UserEntity item:userEntityList
//        ) {
//            ids.add(item.getUserId());
//        }
//
////        LambdaQueryWrapper<ComplainEntity> queryWrapper2=new LambdaQueryWrapper<>();
////        queryWrapper2.like(StringUtils.isNotBlank(master_name),ComplainEntity::getMaster,master_name);
//
//
//        //状态
//        LambdaQueryWrapper<ComplainEntity> queryWrapper1=new LambdaQueryWrapper<>();
//        queryWrapper1.eq(status!=null, ComplainEntity::getStatus,status).eq(master_name!=null, ComplainEntity::getMaster,master_name);
////        queryWrapper1.eq(master_name!=null, ComplainEntity::getMaster,master_name);
//        queryWrapper1.in(ComplainEntity::getUserId,ids);
//
//        //分页
//        Page<ComplainEntity> pageUtil=new Page<>(page,pageSize);
//        Page<ComplainEntity> complainEntityPage = complainService.page(pageUtil, queryWrapper1);
//
//
//
//
//        return R.ok().put("page", complainEntityPage);
//    }

    @GetMapping("/getComplainList")
    public R getComplainList(String userName, Integer status,int page,int pageSize){

        LambdaQueryWrapper<UserEntity> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(userName),UserEntity::getUsername,userName);

        List<UserEntity> userEntityList = userService.list(queryWrapper);

        List<Long> ids=new ArrayList<>();

        for (UserEntity item:userEntityList
        ) {
            ids.add(item.getUserId());
        }

//        LambdaQueryWrapper<ComplainEntity> queryWrapper2=new LambdaQueryWrapper<>();
//        queryWrapper2.like(StringUtils.isNotBlank(master_name),ComplainEntity::getMaster,master_name);
//        queryWrapper2.eq(master_name!=null, ComplainEntity::getMaster,master_name);
//        queryWrapper2.in(ComplainEntity::getUserId,ids);


        //状态
        LambdaQueryWrapper<ComplainEntity> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(status!=null, ComplainEntity::getStatus,status);
//        queryWrapper1.eq(master_name!=null, ComplainEntity::getMaster,master_name);
        queryWrapper1.in(ComplainEntity::getUserId,ids);

//        Page<UserEntity> pageUtil1=new Page<>(page,pageSize);
//        Page<UserEntity> userEntityPage = userService.page(pageUtil1, queryWrapper);


        //分页
        Page<ComplainEntity> pageUtil=new Page<>(page,pageSize);
        Page<ComplainEntity> complainEntityPage = complainService.page(pageUtil, queryWrapper1);

        complainEntityPage.getRecords().forEach(item->{
            if (item.getStatus().equals("0")){
                item.setStatus("未解决");
            }else if (item.getStatus().equals("1")){
                item.setStatus("已解决");
            }
            if (item.getType().equals("0")){
                item.setType("投诉");
            }else if(item.getType().equals("1")){
                item.setType("公告");
            }
        });

        for (ComplainEntity item:complainEntityPage.getRecords()
        ) {
            //根据userid查询用户名
            UserEntity byId = userService.getById(item.getUserId());
            item.setUsername(byId.getUsername());
            item.setNumber(byId.getNumber());
            item.setAddress(byId.getAddress());
        }



        return R.ok().put("page",complainEntityPage);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("xiaoqu:complain:info")
    public R info(@PathVariable("id") Integer id){
		ComplainEntity complain = complainService.getById(id);

        return R.ok().put("complain", complain);
    }

    /**
     * 保存
     */
    @PostMapping("/save")

    public R save(@RequestBody ComplainEntity complain){

//        complain.setDate(new Date());

		complainService.save(complain);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("xiaoqu:complain:update")
    public R update(@RequestBody ComplainEntity complain){
		complainService.updateById(complain);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("xiaoqu:complain:delete")
    public R delete(@RequestBody Integer[] ids){
        System.out.println(ids);
		complainService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @Login
    @GetMapping("/test")
    public R test(@LoginUser UserEntity user){
        return R.ok().put("data",user);
    }
}
